package chapterSystem.client.interfaceDrawing.activities;

import chapterSystem.client.ChapterSystemService;
import chapterSystem.client.callbacks.LoggCallback;
import chapterSystem.client.interfaceDrawing.customWidgets.CalendarWidget;
import chapterSystem.client.model.entitiesInt.Brother;
import chapterSystem.client.model.entitiesInt.ChapterEvent;
import chapterSystem.client.model.enums.ActivityType;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.StackLayoutPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

import java.util.Date;
import java.util.List;

/**
 * User: Hasp
 * Date: 07.04.12
 * Time: 22:51
 */
public class ChapterEventsActivity extends Activity {

    public ChapterEventsActivity(final Brother brother) {
        super(brother, ActivityType.EVENT);
    }

    @Override
    public void drawActivity(final StackLayoutPanel workArea) {
        workArea.clear();
        final CalendarWidget calendar = new CalendarWidget();
        calendar.clickTask = new CalendarWidget.ClickTask() {
            @Override
            public void doTask(CalendarWidget calendar) {
                java.sql.Date dateFrom = new java.sql.Date(calendar.getDate().getTime());
                java.sql.Date dateTo = new java.sql.Date(dateFrom.getTime() + 24*3600L*1000);

                ChapterSystemService.App.getInstance().getEvents(100, dateFrom, dateTo, new GetEventCallback(dateFrom, workArea));
            }
        };
        workArea.add(calendar, new HTML("Events calendar"), 4);

        ChapterSystemService.App.getInstance().addLog(brother.getId(), id, "Review events",
                new LoggCallback());
    }

    private class GetEventCallback implements AsyncCallback<List<ChapterEvent>> {
        private final Date myDate;
        private final StackLayoutPanel workArea;

        private GetEventCallback(Date myDate, StackLayoutPanel workArea) {
            this.myDate = myDate;
            this.workArea = workArea;
        }

        @Override
        public void onFailure(Throwable caught) {
            Window.alert(caught.getMessage());
        }

        @Override
        public void onSuccess(List<ChapterEvent> result) {
            new SeeOneDayEventsActivity(brother, ActivityType.EVENT, result, ChapterEventsActivity.this, myDate).drawActivity(workArea);
        }
    }
}
